package com.guanyong.admin.controllers.api;

import com.guanyong.admin.handle.UploadFileType;
import com.guanyong.model.BannerListModel;
import com.guanyong.services.BannerListService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.utils.NetUtils.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mxk on 2017/10/18.
 */
@RestController
@RequestMapping("/api/banner/")
public class BannerListApi {

    @Autowired
    UploadFileUtil uploadFileUtil;


    @Autowired
    BannerListService bannerListService;


    @RequestMapping(value = "update", method = RequestMethod.POST)
    ResultObject<Integer> updateBanner(HttpServletRequest request,
                                             @RequestParam Integer fid ,
                                             @RequestParam String  url ,
                                             @RequestParam String  title,
                                             @RequestParam(value = "film_pic",required = false) MultipartFile
                                                     photo) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        //单个修改
        BannerListModel bannerListModel=new BannerListModel();
        String path="";
        try {
            path = uploadFileUtil.SaveFileByMultiFlie(photo,request, UploadFileType.banner);
            //path = uploadFileUtil.SaveStreamFile(UploadFileType.Photo, ".jpg", request);
        } catch (Exception e) {
            e.printStackTrace();
            path="";
        }
        bannerListModel.setfId(fid);
        bannerListModel.setfPhoto(path);
        bannerListModel.setfTitle(title);
        bannerListModel.setfUrl(url);
        res=bannerListService.UpdateBanner(bannerListModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }


    @RequestMapping(value = "add", method = RequestMethod.POST)
    ResultObject<Integer> addBanner(HttpServletRequest request,
                                             @RequestParam String  url ,
                                             @RequestParam String  title,
                                             @RequestParam(value = "film_pic",required = false) MultipartFile
                                                     photo) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        //单个修改
        BannerListModel bannerListModel=new BannerListModel();
        String path="";
        try {
            path = uploadFileUtil.SaveFileByMultiFlie(photo,request, UploadFileType.banner);
            //path = uploadFileUtil.SaveStreamFile(UploadFileType.Photo, ".jpg", request);
        } catch (Exception e) {
            e.printStackTrace();
            path="";
        }
        if (path!="")
        {
            bannerListModel.setfPhoto(path);
        }
        bannerListModel.setfId(0);
        bannerListModel.setfTitle(title);
        bannerListModel.setfUrl(url);
        res=bannerListService.AddBanner(bannerListModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }


    @RequestMapping(value = "getalllist", method = RequestMethod.GET)
    ResultObject<    List<BannerListModel> > getAllList(HttpServletRequest request) {
        ResultObject<    List<BannerListModel> > resultObject=new ResultObject<    List<BannerListModel> >();
        List<BannerListModel> list=bannerListService.GetAll();
        resultObject.setResult(list);
        return resultObject;
    }


    @RequestMapping(value = "delbanner", method = RequestMethod.POST)
    ResultObject<Integer> deleteBanner(HttpServletRequest request ,
                                          @RequestParam Integer id) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        int res= bannerListService.DleteBanner(id);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }


    @RequestMapping(value = "top ", method = RequestMethod.POST)
    ResultObject<Integer> updateBannertime(HttpServletRequest request,
                                           @RequestParam Integer id
                                       ) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        int res= bannerListService.UpdateIntime(id);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }
}
